﻿//using LumiSoft.Net.STUN.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace library
{
    static class p2pServer
    {
        static UdpClient server;

        static IPEndPoint localEndPoint;

        #region Thread Refresh

        static Thread thread;

        internal static void Start()
        {
            thread = new Thread(Configure);

            thread.Start();
        }

        internal static void Stop()
        {
            server.Close();
        }

        static void Configure()
        {
            localEndPoint = new IPEndPoint(IPAddress.Any, Client.P2pPort);

            server = new UdpClient();

            server.Client.ExclusiveAddressUse = false;

            server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            server.Client.Bind(localEndPoint);

            // Client.localPeer.EndPoint = STUN_Client.Query("stunserver.org", 3478, client.Client).PublicEndPoint;

            ThreadReceive();
        }

        static void ThreadReceive()
        {
            while (!Client.Stop)
            {
                try
                {
                    IPEndPoint remoteEndPoint = null;

                    byte[] buffer = server.Receive(ref remoteEndPoint);

                    if (remoteEndPoint != null)
                    {
                        Log.Write("<<<    " + remoteEndPoint.Port + "    " + Utils.Points(buffer.Take(30)));

                        Peer peer = Peers.GetPeerOrCreate(remoteEndPoint);

                        p2pResponse.Process(new p2pRequest(peer, buffer));

                        //ThreadPool.QueueUserWorkItem(new WaitCallback(p2pResponse.Process), new p2pRequest(remoteEndPoint, buffer));

                        Client.Stats.Received.Add(buffer.Length);
                    }
                }
                catch { }
            }

            server.Close();
        }

        #endregion
    }
}

